package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.StrategyComment;
import cn.wolfcode.trip.base.domain.Tag;
import cn.wolfcode.trip.base.mapper.StrategyCommentMapper;
import cn.wolfcode.trip.base.mapper.TagMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IStrategyCommentService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Copyright: Copyright (c) 2019 Super520
 * 描述：
 *
 * @package: cn.wolfcode.trip.base.service.impl
 * @author: Patch
 * @date: 2019/5/27 13:57
 * 总结:
 */
@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService {
    @Autowired
    private StrategyCommentMapper commentMapper;
    @Autowired
    private TagMapper tagMapper;

    @Override
    public void saveOrUpdate(StrategyComment comment, String[] tags) {
        if (comment.getId() != null) {
            commentMapper.updateByPrimaryKey(comment);

        } else {
            // 设置创建时间
            comment.setCreateTime(new Date());
            commentMapper.insert(comment);
            for (String name : tags) {
                Tag tag = new Tag();
                tag.setName(name);
                // 保存标签
                tagMapper.insert(tag);
                // 中间表关联
                commentMapper.insertRelation(tag.getId(), comment.getId());
            }

        }
    }

    @Override
    public PageInfo<StrategyComment> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<StrategyComment> list = commentMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public void updateState(StrategyComment comment) {
        commentMapper.updateState(comment.getId(), comment.getState());
    }

    @Override
    public StrategyComment getByStrategyIdAndCommentId(QueryObject qo) {
        return commentMapper.getByStrategyIdAndCommentId(qo);
    }

    @Override
    public List<StrategyComment> listStrategyComment(QueryObject qo) {
        return commentMapper.selectStrtegyUserId(qo);
    }

    @Override
    public StrategyComment getCommentDetail(Long commentId) {
        return commentMapper.selectCommentDetail(commentId);
    }

    @Override
    public PageInfo<StrategyComment> queryForApp(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<StrategyComment> list = commentMapper.selectForAppList(qo);
        return new PageInfo<>(list);
    }


}
